package control;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;

import javax.swing.JTextField;

import com.pallas.swing.date.DateComboBox;

import model.AnhaengerTyp;
import model.Tarif;

public class ControlTarifWahlSuchen
{
	DatabaseAbfragen database;
	ResultSet rs = null;
	private final static int port = 0xCafe;
	private Socket socketToHost = null;
	private ObjectOutputStream os = null;
	private ObjectInputStream in = null;
	private Tarif tarife;
	
	
	public ControlTarifWahlSuchen()
	{

	}

	public void closeAll()
	{
		try
		{
			if (os != null)
			{
				os.close();
			}

			if (in != null)
			{
				in.close();
			}

			if (socketToHost != null)
			{
				socketToHost.close();
			}
		} catch (IOException e)
		{
			System.out.println(e);
		}

		socketToHost = null;
		os = null;
		in = null;
	}

	boolean connectToHost()
	{
		if (socketToHost != null)
		{
			return true;
		}

		try
		{
			String host = InetAddress.getByName(DatabaseConnect.standardIP).getHostName();

			socketToHost = new Socket(host, port);

			os = new ObjectOutputStream(socketToHost.getOutputStream());
			in = new ObjectInputStream(socketToHost.getInputStream());

			return true;
		} catch (UnknownHostException e)
		{
			System.err.println(e);
		} catch (IOException e)
		{
			System.err.println(e);
		}catch (Exception e)
		{
			System.err.println(e);
		}

		closeAll();

		return false;
	}

	public Tarif getTarif(String atyp, Date dateFrom, Date dateTo, String days)
	{

		String aTypS = atyp.substring(0,1);
		System.out.println(aTypS);
		long difference = dateTo.getTime() - dateFrom.getTime();
		int diffTage = (int)(difference / (1000 * 60 * 60 * 24));
		System.out.println(diffTage);
		if(diffTage < 2)
		{
			aTypS = aTypS+"01";
		}else if(diffTage > 3 && diffTage < 6)
		{
			aTypS = aTypS+"02";
		}else if(diffTage > 6)
		{
			aTypS = aTypS+"03";
		}
		
		//aTypS = "E01";
		String statement = new String("sSELECT Bezeichnung, Betrag, GueltigVon, GueltigBis, TarifID FROM tarif WHERE TarifID = '" + aTypS + "'");
		
		System.out.println(statement);
		tarife = getTarifVerarbeitung(statement);
		return tarife;
	}
	
	public Tarif getTarifVerarbeitung(String statement)
	{
		if( !connectToHost(  ) ) {
            return tarife;
        }
		try {
			os.writeObject(statement);
			os.flush();
			rs = (ResultSet) in.readObject();
                        
        } catch( IOException | ClassNotFoundException e ) {
            System.err.println( e );
        }finally
        {
        	closeAll();
        }
		
		database = new DatabaseAbfragen();
		
		if (rs != null)
		{

			try
			{
				rs.beforeFirst();
				while (rs.next())
				{

					this.tarife = new Tarif(rs
							.getString("Bezeichnung"), rs
							.getDouble("Betrag"), rs
							.getDate("GueltigVon"), rs.getDate("GueltigBis"), rs.getString("TarifID"));


				}
			} catch (SQLException e)
			{
				e.printStackTrace();

			}
		}
		return tarife;
	}
}
